import requests
import spider
import urllib
from spider import common
import random

class GoodsSpider():
    def __init__(self):
        self.url = "https://h5.ele.me/pizza/shopping/restaurants/%s/batch_shop"
        self.searchUrl = "https://h5.ele.me/restapi/shopping/v1/typeahead"
        self.searchParam={
            "latitude": "22.574064",
            "longitude": "113.958347",
            "city_id": "11",
            "kw":""
        }

        self.param = {
            "latitude": "22.574064",
            "longitude": "113.958347",
            "extras": ["activities","albums","license","identification","qualification"],
            "terminal": "h5",
        }

        self.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36',
            "referer": "https://h5.ele.me/shop/"
        }

        self.proxies = [
            "103.251.87.56:868",
            "116.227.128.77:37439",
            "211.159.171.58:80",
            "203.130.46.108:9090",
            "134.196.91.18:41177",
            "14.20.235.106:9797",
            "183.236.238.6:54152",
            "103.59.213.26:8080",
            "114.116.10.21:3128",
            "119.123.172.157:9797",
            "202.116.162.35:8008",
            "45.221.73.46:8080",
            "120.83.49.72:9000"
        ]



    def send(self,url,param=None,headers=None):
        while(True):
            prox = random.sample(self.proxies,1)[0]
            proxies={
                'https': 'https://%s'%prox
            }
            #proxies = None
            try:
                res = requests.get(url,param,headers=headers,proxies=proxies,timeout=10)
                if res.status_code == 200:
                    return res
                else:
                    print("状态码 %s"%str(res.status_code))
                    #return None
            except Exception as e:
                print("^^^^^^^^^^^^^^^^^^^^^^^^^^^")
                print(e)
                print("^^^^^^^^^^^^^^^^^^^^^^^^^^^")
            print("准备重连-------------------")


    def run(self):
       self.start();


    def start(self):
        cursor = spider.db.cursor();
        cursor.execute("select a.id ,a.elm_id ,a.spiderGoods,a.name from t_resturant a")
        all_data = cursor.fetchall()
        for item in all_data:
            spiderGoods = item.get("spiderGoods")
            if spiderGoods == 1:
                continue
            elm_id = item.get("elm_id")
            id= item.get("id")
            name = item.get("name")

            #re_id = self.search(name)
            re_id = item.get("elm_id")
            if not re_id:
                print ("#######跳过%s"%name)
                continue
            url = self.url % re_id
            self.getGoodsData(url,id)
            update_sql = "update t_resturant a set a.spiderGoods = %s,a.elm_id = '%s' where a.id = %s"%("1",re_id,id)
            spider.excSql(update_sql)
            print ("*********** %s"%(name))


    def getGoodsData(self,url,res_id):
        res = self.send(url, self.param, self.headers);
        data = res.json();
        menus = data.get("menu",[])
        for menu in menus:
            goodsType={
                "description":menu.get("description",""),
                "name":menu.get("name",""),
                "resturant_id":res_id
            }
            goods_type_sql = spider.get_insert_sql(goodsType,"t_goods_type")
            spider.excSql(goods_type_sql)
            spider.cursor.execute("select last_insert_id() as id")
            goods_type_id = spider.cursor.fetchone().get("id")

            foods = menu.get("foods",[])
            self.goodsHandle(foods,goods_type_id)





    def search(self,name):
        self.searchParam["kw"] = name
        res = self.send(self.searchUrl,self.searchParam,spider.headers)
        data = res.json()
        restaurants = data.get("restaurants",[])
        if(len(restaurants)<1):
            return None;
        return restaurants[0].get("id",None)


    def goodsHandle(self,goods_list,type_id):
        all_sql = []
        for goods in goods_list:
            specfoods = goods.get("specfoods",[])[0]
            price = specfoods.get("price","0")
            month_sales = goods.get("month_sales", "0")
            if not month_sales:
                month_sales = "0"
            goods_dic = {
                "name":goods.get("name"),
                "description":goods.get("description",""),
                "img":common.img_handle_elm(goods.get("image_path")),
                "month_sales":month_sales,
                "goods_type":type_id,
                "price":price
            }
            sql = spider.get_insert_sql(goods_dic,"t_goods")
            all_sql.append(sql)
        spider.excSqls(all_sql)




    def getResturantInfo(self,categer,container_categorie):
        id = categer.get("id")
        param =  self.param;
        param["restaurant_category_ids"] = [id]
        res = self.send(self.url,param,headers=spider.headers)
        datas = res.json().get("items");
        result = {}
        for data in datas:
            data = data.get("restaurant")
            image_path = data.get("image_path")
            img_url = common.img_handle_elm(image_path)

            resturant_info = {
                "authentic_id":data.get("authentic_id"), #权限？
                "address": data.get("address"),  #地址
                "baidu_id": data.get("baidu_id"), #好像查询物品得到id
                "float_delivery_fee": data.get("float_delivery_fee"), #配送费用
                "float_minimum_order_amount": data.get("float_minimum_order_amount"),  # 多少起送
                "elm_id": data.get("id"), #饿了么的主键吧
                "name": data.get("name"),
                "rating": data.get("rating"), #多少星
                "recent_order_num": data.get("recent_order_num"), #月销量
                "img":img_url+"?imageMogr/format/webp/thumbnail/!130x130r/gravity/Center/crop/130x130/",
            }

            channel_list = []
            flavors = data.get("flavors")

            for flavor in flavors:
                channel = {
                    "resturant_id":resturant_info.get("elm_id"),
                    "categories_id":flavor.get("id")
                }
                channel_list.append(channel)
                categories = {
                    "elm_id":flavor.get("id"),
                     "name":flavor.get("name")
                }
                self.allCategories[flavor.get("id")] = categories
                container_categorie[flavor.get("id")] = categories
             #插入数据库
            self.saveResturant(resturant_info,channel_list)



    def saveResturant(self,resturant_info,channel_list):
        elm_id = resturant_info.get("elm_id")
        resturant_count =  spider.cursor.execute("select * from t_resturant a where a.elm_id = '%s'"%str(elm_id))
        if resturant_count != 0:
            return None;
        sql_list = []
        resturant_sql = spider.get_insert_sql(resturant_info,"t_resturant")
        sql_list.append(resturant_sql)

        for channel in channel_list:
            resturantSql = spider.get_insert_sql(channel, "t_channel_cat2res")
            sql_list.append(resturantSql)

        spider.excSqls(sql_list)